package com.dzy.chiyan.fragment

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageButton
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.dzy.chiyan.R
import com.dzy.chiyan.adapter.Friend
import com.dzy.chiyan.adapter.FriendAdapter
import com.dzy.chiyan.data.DBHelper
import com.dzy.chiyan.data.FriendshipDAOImpl
import com.dzy.chiyan.data.UserDaoImpl

// 管理用户好友列表，添加好友
class FriendFragment(private val userID: Int) : Fragment() {
    // RecyclerView用于显示好友列表
    private lateinit var recyclerView: RecyclerView
    // FriendAdapter为RecyclerView提供数据
    private lateinit var adapter: FriendAdapter
    private lateinit var context: Context

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_friend, container, false)
        recyclerView = view.findViewById(R.id.recyclerView)
        // 获取 ImageButton 对象
        val addFriendButton: ImageButton = view.findViewById(R.id.imageBut_addFriend)
        // 添加好友点击事件
        addFriendButton.setOnClickListener {
            onAddFriend()
        }
        context = requireContext()
        val layoutManager = LinearLayoutManager(context)
        recyclerView.layoutManager = layoutManager

        // 初始化FriendAdapter，传入一个空的好友列表和当前用户ID
        adapter = FriendAdapter(mutableListOf<Friend>(), userID)
        recyclerView.adapter = adapter
        loadData()
        return view
    }
    // 加载好友数据，调用数据库查询
    private fun loadData() {
        // 加载用户的好友列表
        val dao = FriendshipDAOImpl(DBHelper(context))
        adapter.reloadData(dao.findFriendById(userID))
    }

    // 添加好友
    private fun onAddFriend() {
        val alertDialog = AlertDialog.Builder(context)
        // 创建一个EditText用于输入账号
        val input = EditText(context)
        alertDialog.setView(input)
        // 设置对话框的标题和提示
        alertDialog.setTitle("添加好友")
        alertDialog.setMessage("请您输入好友账号")
        alertDialog.setPositiveButton("确认") { dialog, which ->
            val friendName = input.text.toString()
            // 判断账号是否是11位数字（手机号）
            if (friendName.matches(Regex("\\d{11}"))) {
                val db = DBHelper(context)
                // 创建FriendshipDAOImpl操作好友关系、UserDaoImpl用户数据
                val friendshipDAOImpl = FriendshipDAOImpl(db)
                val user = UserDaoImpl(db)
                // 查找该账号对应的用户ID
                val friendID = user.getIdByUsername(friendName) ?: 0
                val message: String
                if (friendshipDAOImpl.add(userID, friendID)) {
                    message = "添加成功"
                    loadData()
                } else {
                    message = "添加失败"
                }
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
            } else {
                // 输入错误
                Toast.makeText(context, "账号输入错误", Toast.LENGTH_SHORT).show()
            }
        }
        alertDialog.setNegativeButton("取消") { dialog, which ->
            dialog.cancel()
        }
        alertDialog.show()
    }
}